package com.wonders.ssm.controller;

import com.wonders.ssm.common.CommonResult;
import com.wonders.ssm.constant.Message;
import com.wonders.ssm.pojo.AuthUser;
import com.wonders.ssm.pojo.Student;
import com.wonders.ssm.service.StudentService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created by lv on 2018/7/27.
 */
@RestController
public class DataController {

    private Logger logger = Logger.getLogger(getClass());

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "student.json")
    public Student selectByPrimaryKey(){
        Student student = this.studentService.selectByPrimaryKey("5143");
        return student;
    }

    @RequestMapping(value = "wage.json")
    public Integer wage(){
        int a = 0;
        try {
            Student student = new Student();
            student.setId("0001");
            student.setName("张三");
            student.setPassword("123456");
            student.setRegdate(new Date());
            a = studentService.insertWage(student);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return a;
    }

    /**
     * 用户登录
     * @param name
     * @param password
     * @return
     */
    @RequestMapping(value = "login.json")
    public CommonResult login(String name, String password,String imageCode,HttpSession session){
        CommonResult result = new CommonResult();
        try {
            String sessionKeyCode =  (String)session.getAttribute("sessionKeyCode");
            if (sessionKeyCode.equalsIgnoreCase(imageCode)){
                AuthUser authUser = studentService.login(name,password);
                if (authUser != null){
                    result.setCode(Message.SUCCESS_CODE);
                    result.setMessage(Message.SUCCESS_MESSAGE);
                    result.putData("data",authUser);
                }else{
                    Message.setNullMessage(result);
                }
            }else{
                Message.setImageCodeMessage(result);
            }
        } catch (Exception e) {
            Message.setErrorMessage(result,e.getMessage());
            logger.error(e.getMessage(),e.getCause());
            e.printStackTrace();
        }
        return result;
    }
}
